<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/class_address.php');
include('class/class_workdays.php');
include('class/class_rating.php');
include('class/abstract/class_mainObjectInfo.php');
include('class/city_rus.php');
include('class/class_checkGetData.php');
include('includes/transliterate.php');
$aCheck=new checkGetData('m');
$aCheck->checkTableId($_GET['city'], $_GET['id']);
foreach($_GET as $key=>$vol)
	$_GET[$key]=$aCheck->safetyCheck($vol);
foreach($_POST as $key=>$vol)
{	if($key=='firmToAdd')continue;
	$_POST[$key]=$aCheck->safetyCheck($vol);
}
session_start();
if(!$_SESSION['login'])
{	header('location: registr.php?city='.$_GET['city']);
	exit;}
$aConnect=new connect('m');
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
if(strtolower($_SESSION['login'])!=strtolower($row['user']))
	checkGetData::error();
if($_POST['addMallsFirmsButton'])
{	if($_POST['firmToAdd'])
	{		$newMallsFirms=array();		$ma=explode(', ', $row['address']);
		$mallsStreet=$ma[0];
		$mallsBuilding=$ma[1];		$aConnect=new connect('f');		foreach($_POST['firmToAdd'] as $key=>$firm)
		{			$newAddr=array();
			$newAddress=array();			$firmsEls=explode('^', $firm);
			$firmsId=$firmsEls[0];
			$r=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$firmsId."'"));
			if($r)
			{				$address=explode('|', $r['address']);
				foreach($address as $k=>$addr)
				{                	$addrEls=explode('^', $addr);
                	if($mallsStreet==$addrEls[0] && $mallsBuilding==$addrEls[1])
                	{                		$addrEls[3]=$row['id'].','.$row['name'].','.$row['short'];                	}
                	if(is_array($addrEls))$newAddr[]=implode('^', $addrEls);
                }
				if(is_array($newAddr))$newAddress=implode('|', $newAddr);
				($r['malls'])?$newMalls=$r['malls'].','.$row['id']:$newMalls=$row['id'];
				$dataToUpdate="address='".$newAddress."', malls='".$newMalls."'";
				$aConnect->update_data($_GET['city'], $dataToUpdate, "WHERE id='".$r['id']."'");
				$newMallsFirms[]=$r['id'].'^'.$r['name'].'^'.$r['subdiv'];			}		}
		if($row['firms'] && $newMallsFirms)$newFirms=$row['firms'].'|'.implode('|', $newMallsFirms);
		else $newFirms=implode('|', $newMallsFirms);
		$aConnect=new connect('m');
		$aConnect->update_data($_GET['city'], "firms='".$newFirms."'", "WHERE id='".$_GET['id']."'");
		header('location: mallscabinet.php?city='.$_GET['city'].'&id='.$_GET['id']);
		exit;
	}}
class aFirmPage extends page
{
	var $row=array();
	var $cityRus;
	function __construct($row)
	{		$this->row=$row;
		$this->cityRus=new city_rus_class();
		parent::__construct();
	}
	function title()
	{
		echo $this->row['name'].' '.$this->cityRus->city_rus_im($_GET['city']);
	}
	function maincontent()
	{
		echo ('
		<link rel="stylesheet" type="text/css" href="style/addMallsFirms.css" />
		<script type="text/javascript" src="scripts/addmallsfirms.js">
		</script>
		<div class="route">
			<div class="routeEl">
				<a href="pcabinet.php?city='.$_GET['city'].'">Личный кабинет</a>
			</div>
			<div class="routeEl">
				<a href="my_malls.php?city='.$_GET['city'].'">Мои центры</a>
			</div>
			<div class="routeEl">
				<a href="mallscabinet.php?city='.$_GET['city'].'&id='.$_GET['id'].'">'.$this->row['name'].'</a>
			</div>
			<div class="routeEl">
				Добавить фирмы
			</div>
		</div>
		<div id="formInfo">
			Здесь вы можете добавить в ваш центр новые фирмы<br />
			<div id="showHiddenInfo">
				Подробнее
			</div>
			<div id="hiddenInfo">
					Вы можете указать находящиеся в вашем центре фирмы, зарегестрированные на нашем сайте по адресу вашего центра,
					но еще не отмеченные вами или представителями самих фирм.
				<div id="hideHiddenInfo">
					Скрыть
				</div>
			</div>
		</div>
		<center><h4 id="formHeader">Добавьте фирмы в '.$this->row['short'].' '.$this->row['name'].'</h4></center>
		');
		$mallsFirmsIds=array();
		$firms=explode('|', $this->row['firms']);
		foreach($firms as $key=>$firm)
		{			$firmsEls=explode('^', $firm);			$mallsFirmsIds[]=$firmsEls[0];		}
		$address=explode(', ',$this->row['address']);
		$street=$address[0];
		$building=$address[1];
		$aConnect=new connect('f');
		$res=$aConnect->select_all($_GET['city'], "WHERE address LIKE '%".$street."^".$building."^%'");
		while($row=mysql_fetch_array($res))
		{			if(!in_array($row['id'], $mallsFirmsIds))
			{				$addresses=explode('|', $row['address']);
				foreach($addresses as $key=>$addr)
				{					$addrEls=explode('^', $addr);					if($street==$addrEls[0] && $building==$addrEls[1])
					$unaddedFirms[]=$row['id'].'^'.$row['name'].'^'.$row['subdiv'];				}			}		}
		if($unaddedFirms)
		{			echo ('
			<div class="addMallsFirms">
			<center>
				<form action="" method="post" id="addMallsFirmsForm" name="addMallsFirmsForm">
				<table>
			');
			if(count($unaddedFirms)>1)
				{
					echo ('
					<tr>
						<td colspan="3" align="left">
							<input name="checkAll" id="checkAll" type="checkbox" value="checkAll">
							Выбрать все
						</td>
					</tr>
					');
				}
			foreach($unaddedFirms as $key=>$firms)
			{				$firm=explode('^', $firms);
				echo ('
				<tr class="firmToAdd">
					<td class="check">
						<input name="firmToAdd[]" class="firmToAddInput" type="checkbox" value="'.$firm[0].'^'.$firm[1].'^'.$firm[2].'">
					</td>
					<td class="fName">
						'.$firm[1].'
					</td>
					<td class="fImg">
					');
					if(file_exists('pictures/firmspics/'.$_GET['city'].'/'.$firm[0].'.jpg'))
						echo '<img src="pictures/firmspics/'.$_GET['city'].'/'.$firm[0].'.jpg" />';
					echo ('
					</td>
				</tr>
				');			}
			echo ('
				</table>
				<input class="addbutton" value="Добавить" name="addMallsFirmsButton" type="submit" />
				</form>
			</center>
			</div>
			');
		}
		else
		{			echo ('
			<center>
				<h3>
					Все фирмы, зарегистрированные на сайте по адресу '.$this->row['address'].' добавлены!
				</h3>
			</center>
			');		}
	}
}

$aPage=new aFirmPage($row);

?>